// $ANTLR : "Traductor.g" -> "Traductor.java"$

	package procesador.tablas;
	
	import procesador.Tabla;
	import procesador.Campo;
	import java.util.List;
	import java.util.ArrayList;
	import java.util.LinkedList;

import antlr.TreeParser;
import antlr.Token;
import antlr.collections.AST;
import antlr.RecognitionException;
import antlr.ANTLRException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.collections.impl.BitSet;
import antlr.ASTPair;
import antlr.collections.impl.ASTArray;


public class Traductor extends antlr.TreeParser       implements TraductorTokenTypes
 {

	private List<Tabla> tablas = new ArrayList<Tabla>();
	
	public List<Tabla> getTablas(){
		return this.tablas;
	}
public Traductor() {
	tokenNames = _tokenNames;
}

	public final void tablas(AST _t) throws RecognitionException {
		
		AST tablas_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		
		try {      // for error handling
			AST __t2629 = _t;
			AST tmp1_AST_in = (AST)_t;
			match(_t,TABLAS);
			_t = _t.getFirstChild();
			{
			_loop2631:
			do {
				if (_t==null) _t=ASTNULL;
				if ((_t.getType()==IDENT)) {
					tabla(_t);
					_t = _retTree;
				}
				else {
					break _loop2631;
				}
				
			} while (true);
			}
			_t = __t2629;
			_t = _t.getNextSibling();
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final void tabla(AST _t) throws RecognitionException {
		
		AST tabla_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		AST i = null;
		Tabla tabla;
		
		try {      // for error handling
			AST __t2633 = _t;
			i = _t==ASTNULL ? null :(AST)_t;
			match(_t,IDENT);
			_t = _t.getFirstChild();
			tabla = new Tabla(i.getText());
			campos(_t,tabla);
			_t = _retTree;
			claves(_t,tabla);
			_t = _retTree;
			registros(_t,tabla);
			_t = _retTree;
			_t = __t2633;
			_t = _t.getNextSibling();
			
						tablas.add(tabla);
						//System.out.println("\tTabla procesada: " + tabla.getNombre());
					
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final void campos(AST _t,
		Tabla tabla
	) throws RecognitionException {
		
		AST campos_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		
		try {      // for error handling
			AST __t2635 = _t;
			AST tmp2_AST_in = (AST)_t;
			match(_t,CAMPOS);
			_t = _t.getFirstChild();
			{
			_loop2637:
			do {
				if (_t==null) _t=ASTNULL;
				if ((_t.getType()==CAMPO)) {
					campo(_t,tabla);
					_t = _retTree;
				}
				else {
					break _loop2637;
				}
				
			} while (true);
			}
			_t = __t2635;
			_t = _t.getNextSibling();
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final void claves(AST _t,
		Tabla tabla
	) throws RecognitionException {
		
		AST claves_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		AST i = null;
		
		try {      // for error handling
			AST __t2642 = _t;
			AST tmp3_AST_in = (AST)_t;
			match(_t,PRIMARYKEY);
			_t = _t.getFirstChild();
			{
			int _cnt2644=0;
			_loop2644:
			do {
				if (_t==null) _t=ASTNULL;
				if ((_t.getType()==IDENT)) {
					i = (AST)_t;
					match(_t,IDENT);
					_t = _t.getNextSibling();
					tabla.addPrimaryKey(i.getText());
				}
				else {
					if ( _cnt2644>=1 ) { break _loop2644; } else {throw new NoViableAltException(_t);}
				}
				
				_cnt2644++;
			} while (true);
			}
			_t = __t2642;
			_t = _t.getNextSibling();
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final void registros(AST _t,
		Tabla tabla
	) throws RecognitionException {
		
		AST registros_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		
		try {      // for error handling
			AST __t2646 = _t;
			AST tmp4_AST_in = (AST)_t;
			match(_t,REGISTROS);
			_t = _t.getFirstChild();
			{
			_loop2648:
			do {
				if (_t==null) _t=ASTNULL;
				if ((_t.getType()==REGISTRO)) {
					registro(_t,tabla);
					_t = _retTree;
				}
				else {
					break _loop2648;
				}
				
			} while (true);
			}
			_t = __t2646;
			_t = _t.getNextSibling();
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final void campo(AST _t,
		Tabla tabla
	) throws RecognitionException {
		
		AST campo_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		AST i = null;
		String t=null;
		
		try {      // for error handling
			AST __t2639 = _t;
			AST tmp5_AST_in = (AST)_t;
			match(_t,CAMPO);
			_t = _t.getFirstChild();
			i = (AST)_t;
			match(_t,IDENT);
			_t = _t.getNextSibling();
			t=tipo(_t);
			_t = _retTree;
			_t = __t2639;
			_t = _t.getNextSibling();
			
										tabla.addCampo(new Campo(i.getText(), t));
					
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final String  tipo(AST _t) throws RecognitionException {
		String res=null;
		
		AST tipo_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		
		try {      // for error handling
			if (_t==null) _t=ASTNULL;
			switch ( _t.getType()) {
			case NUMERIC:
			{
				AST tmp6_AST_in = (AST)_t;
				match(_t,NUMERIC);
				_t = _t.getNextSibling();
				res = Campo.CAMPO_NUMERICO;
				break;
			}
			case CHAR:
			{
				AST tmp7_AST_in = (AST)_t;
				match(_t,CHAR);
				_t = _t.getNextSibling();
				res = Campo.CAMPO_TEXTO;
				break;
			}
			default:
			{
				throw new NoViableAltException(_t);
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
		return res;
	}
	
	public final void registro(AST _t,
		Tabla tabla
	) throws RecognitionException {
		
		AST registro_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		List<Object> atr = new LinkedList<Object>();
		
		try {      // for error handling
			AST __t2650 = _t;
			AST tmp8_AST_in = (AST)_t;
			match(_t,REGISTRO);
			_t = _t.getFirstChild();
			{
			_loop2652:
			do {
				if (_t==null) _t=ASTNULL;
				if ((_t.getType()==CADENA||_t.getType()==NUMERO)) {
					atributo(_t,atr);
					_t = _retTree;
				}
				else {
					break _loop2652;
				}
				
			} while (true);
			}
			_t = __t2650;
			_t = _t.getNextSibling();
			tabla.addRegistro(atr);
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final void atributo(AST _t,
		List<Object> atr
	) throws RecognitionException {
		
		AST atributo_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		AST c = null;
		AST n = null;
		
		try {      // for error handling
			if (_t==null) _t=ASTNULL;
			switch ( _t.getType()) {
			case CADENA:
			{
				c = (AST)_t;
				match(_t,CADENA);
				_t = _t.getNextSibling();
				atr.add(c.getText());
				break;
			}
			case NUMERO:
			{
				n = (AST)_t;
				match(_t,NUMERO);
				_t = _t.getNextSibling();
				atr.add(new Double(n.getText()));
				break;
			}
			default:
			{
				throw new NoViableAltException(_t);
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	
	public static final String[] _tokenNames = {
		"<0>",
		"EOF",
		"<2>",
		"NULL_TREE_LOOKAHEAD",
		"TABLAS",
		"CAMPOS",
		"CAMPO",
		"REGISTROS",
		"REGISTRO",
		"PRIMARYKEY",
		"\"@\"",
		"IDENT",
		"\"(\"",
		"\")\"",
		"\":\"",
		"SALTO_LINEA",
		"\",\"",
		"\"/\"",
		"CHAR",
		"NUMERIC",
		"CADENA",
		"NUMERO"
	};
	
	}
	
